import Vue from 'vue'
import App from './App.vue'
import router from './router'
import 'bootstrap/dist/css/bootstrap.css'
import axios from 'axios'
import VueAxios from 'vue-axios'
import Vant from 'vant';
import 'vant/lib/index.css';
import Vuex from 'vuex';
import store from './store';


Vue.use(VueAxios, axios).use(Vant).use(Vuex);
Vue.config.productionTip = false

axios.defaults.timeout = 50000 //设置请求超时时间
// axios.defaults.baseURL = "" //设置baseURL

//设置axios的响应拦截器
axios.interceptors.response.use(response=>{
  if (response.status === 200 && response.data.userToken){
    localStorage.setItem("userToken",response.data.userToken);
  }
  return response.data;
},error =>{
  if(error.response.status === 409 ){
    localStorage.removeItem("userToken");
    router.push("/login");
  }
  return Promise.reject(error);
});

//设置axios的发送请求拦截器
axios.interceptors.request.use(config=>{
  //每次请求携带token
  if(localStorage.getItem("userToken")){
    config.headers.userAuth = localStorage.getItem("userToken")
  }
  return config;
},error=>{
  return Promise.reject(error);
})

new Vue({
  router,
  render: h => h(App),
  store
}).$mount('#app')
